package com.css.zfzx.sjcj.common.utils;

import com.css.zfzx.sjcj.common.MenuInfoEnum;

import java.util.Arrays;
import java.util.List;

/**
 * @Description: pc-statistics
 * @Author: lhl
 * @CreateDate: 2021/5/14 11:23
 */
public class StrUtils {
    /**
     * 查看menuIds是否存在menuInfoEnum和menuInfoEnum的父级
     *
     * @param menuIds
     * @param menuInfoEnum
     * @return
     */
    public static boolean splitMenuId(List<String> menuIds, MenuInfoEnum menuInfoEnum) {
        //1级——>2级
        //2级——>1级
        //1级——>2级——>3级
        //3级——>2级——>1级
        boolean isHave = false;//是否有该菜单
        if (menuIds.contains("1")) {//判断是否是全部的菜单
            isHave = true;
        }
        //是否包含本身ID
        if (menuIds.contains(menuInfoEnum.getMenuId())) {
            isHave = true;
        }
        //只是二级菜单
        String pid = menuInfoEnum.getPid();
        if (menuIds.contains(pid)) {
            isHave = true;
        }
        //判断是否只是三级菜单
        String rootId = null;
        if (menuInfoEnum.getLevel() == "3") {
            rootId = MenuInfoEnum.getInstanceByName(menuInfoEnum.getPid()).getPid();
        }
        if (menuIds.contains(rootId)) {
            isHave = true;
        }
        return isHave;
    }

    public static boolean geometryIsEqualsCoordinate(String wkt, String lon, String lat) {
        //multipoint (119.302 35.23300000088943)
        String wktPart = "";
        if (wkt.toLowerCase().contains("multipoint")) {
            if (wkt.contains("((")){
                wktPart = wkt.substring(wkt.indexOf("(") + 2, wkt.lastIndexOf(")")-1);
            }else {
                wktPart = wkt.substring(wkt.indexOf("(") + 1, wkt.lastIndexOf(")"));
            }
        }else {
            wktPart = wkt.substring(wkt.indexOf("(") + 1, wkt.lastIndexOf(")"));
        }

        int det = 1 + 6;
        String[] array = wktPart.split(" ");
        String longitude = array[0], latitude = array[1];
        String newWktLongitudeStr = keepDecimalDigits(longitude, det), newWktLatitudeStr = keepDecimalDigits(latitude, det);
        String newLongitudeStr = keepDecimalDigits(lon, det), newLatitudeStr = keepDecimalDigits(lat, det);

        double newWktLongitude = Double.parseDouble(newWktLongitudeStr), newWktLatitude = Double.parseDouble(newWktLatitudeStr);
        double newLongitude = Double.parseDouble(newLongitudeStr), newLatitude = Double.parseDouble(newLatitudeStr);

        if (newWktLongitude == newLongitude && newWktLatitude == newLatitude) return true;
        return false;
    }
    private static double[] bytesToOnePoint(byte[] arr) {
        return new double[]{bytes2Double(arr, 9), bytes2Double(arr, 17)};
    }

    private static double bytes2Double(byte[] arr, int start) {
        long value = 0;
        for (int i = 0; i < 8; i++) {
            value |= ((long) (arr[start + i] & 0xff)) << (8 * i);
        }
        return Double.longBitsToDouble(value);

    }

    private static double[] bytesToMutiPoints(byte[] arr) {
        int len = (arr.length - 13) / 8;
        double[] result = new double[len];
        for (int i = 0; i < len; ++i) {
            result[i] = bytes2Double(arr, 13 + i * 8);
        }
        return result;
    }

    private static String keepDecimalDigits(String floatStr, int det) {
        int index = floatStr.indexOf("."), len = floatStr.length();
        if (index == -1) return floatStr;
        int temp=det+3;
        String tempfloatPart=floatStr.substring(index + 1, (len > (index + temp)) ? (index + temp) : len);
        String floatPart = floatStr.substring(index + 1, (len > (index + det)) ? (index + det) : len);
        if(tempfloatPart.length()>floatPart.length()&&tempfloatPart.startsWith(floatPart)&&tempfloatPart.endsWith("9")){
            int findex=0;
            String sindex="";
            for(int i=floatPart.length()-1;i>0;i--){
                String subStr = floatPart.substring(i, i+1);
                if(subStr.equals("9")){
                    continue;
                }
                findex=i;
                sindex=Integer.toString(Integer.parseInt(subStr)+1);
                break;
            }
            floatPart= floatPart.substring(0, findex)+ sindex;
        }
        String newFloatStr = floatStr.substring(0, index) + "." + floatPart;
        return newFloatStr;
    }

    public static void main(String[] args) {
        //String floatStr="123.45699999999";
    String floatStr="37.129166999999995";
    int det=7;
    String sss=keepDecimalDigits(floatStr,det);
    double dd = Double.parseDouble(sss);
        System.out.println(dd);

    }
}
